package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.google.android.exoplayer2.ui.PlayerView;
import easier.exoplayer.ExoPlayerEasier;

public class MainActivity111 extends AppCompatActivity {

    private static final String TAG = "MainActivity111";

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity111.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main111 );
        initView();
    }

    private void initView() {

        Button button = (Button) findViewById( R.id.button );
        button.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                String play = "play";
                Fragment fragmentByTag = getSupportFragmentManager().findFragmentByTag( play );
                if ( fragmentByTag != null ) {
                    return;
                }
                getSupportFragmentManager().beginTransaction().replace( R.id.container, new PlayFragment(), play ).commit();
            }
        } );

        findViewById( R.id.button1 ).setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                String play = "playFunction";
                Fragment fragmentByTag = getSupportFragmentManager().findFragmentByTag( play );
                if ( fragmentByTag != null ) {
                    return;
                }
                getSupportFragmentManager().beginTransaction().replace( R.id.container, new PlayFunctionFragment(), play )
                    .commit();
            }
        } );
    }

    public static class PlayFunctionFragment extends Fragment {

        private PlayerView playerView;
        private View mock;
        private ImageView play;
        private Button create;
        private Button release;
        private Button playButton;
        private Button pause;
        private Button playing;
        private Button stop;

        @Nullable
        @Override
        public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState ) {

            return inflater.inflate( R.layout.activity_main111_2, container, false );
        }

        @Override
        public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {

            super.onViewCreated( view, savedInstanceState );
            initView( view );

            ExoPlayerEasier easier = new ExoPlayerEasier( playerView );
            easier.setUseController( false );

            Uri uri = Uri.parse( "https://storage.yino2o.com:15030/user/2023-07-11/d9ae85df1086bc202aea439dec5ff084.mp4" );

            create.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    easier.createPlayer();
                    easier.setMediaSourceFromUri( uri );

                    easier.setViewAsPlayButton( play );
                    mock.setOnClickListener( new OnClickListener() {
                        @Override
                        public void onClick( View v ) {

                            play.performClick();
                        }
                    } );
                }
            } );

            release.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    easier.releasePlayer();
                }
            } );

            playButton.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    if ( easier.playerIsIdleState() ) {
                        easier.setMediaSourceFromUri( uri );
                        easier.play();
                    } else {
                        easier.play();
                    }
                }
            } );

            pause.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    easier.pause();
                }
            } );

            playing.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    Toast.makeText( v.getContext(), "" + easier.isPlaying(), Toast.LENGTH_SHORT ).show();
                }
            } );

            stop.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    easier.stop();
                }
            } );
        }

        private void initView( View view ) {

            playerView = (PlayerView) view.findViewById( R.id.playerView );
            mock = (View) view.findViewById( R.id.mock );
            play = (ImageView) view.findViewById( R.id.play );
            create = (Button) view.findViewById( R.id.create );
            release = (Button) view.findViewById( R.id.release );
            playButton = (Button) view.findViewById( R.id.playButton );
            pause = (Button) view.findViewById( R.id.pause );
            playing = (Button) view.findViewById( R.id.playing );
            stop = (Button) view.findViewById( R.id.stop );
        }
    }

    /**
     * 简单的场景播放
     */
    public static class PlayFragment extends Fragment {

        private PlayerView playerView;
        private ImageView play;
        private View mock;

        @Nullable
        @Override
        public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState ) {

            return inflater.inflate( R.layout.activity_main111_1, container, false );
        }

        @Override
        public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {

            super.onViewCreated( view, savedInstanceState );
            initView( view );

            ExoPlayerEasier easier = new ExoPlayerEasier( playerView );
            easier.setUseController( false );

            Uri uri = Uri.parse( "https://storage.yino2o.com:15030/user/2023-07-11/d9ae85df1086bc202aea439dec5ff084.mp4" );

            easier.createPlayer();
            easier.setMediaSourceFromUri( uri );
            easier.play();
            play.setVisibility( View.GONE );

            easier.setViewAsPlayButton( play );
            mock.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    play.performClick();
                }
            } );
        }

        private void initView( View view ) {

            playerView = (PlayerView) view.findViewById( R.id.playerView );
            play = (ImageView) view.findViewById( R.id.play );
            mock = (View) view.findViewById( R.id.mock );
        }
    }
}